<?php
/*
	Sponsor Manager (class.users.php file)
    Copyright (C) <Fri Feb 08 21:37:58 CET 2008>  Marcos Ramírez <marcosramirez@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */
class Users {
	function __construct() {
		
	}
	
	
	function checkLogin ($user, $pass) {
		global $sm;
		$pass=sha1($pass);
		$sm->query("SELECT userid FROM Sys_users WHERE username='$user' AND password='$pass'");
		if ($sm->error()!='')
			return 1;
		if (is_array($Res=$sm->fetch()))
			return 0;
		
		return 1;
	}
	
	function checkUserExists ($user) {
		global $sm;
		$check=$sm->query("SELECT userid FROM Sys_users WHERE username='$user'");
		if ($sm->error()!='')
			return 1;
		if (is_array($Res=$sm->fetch()))
			return true;
		else
			return false;
			
		
	}
	
	
	function addUser ($username, $password, $email) {
		global $sm;
		$username=$sm->sanitizeString($username);
		$password=sha1($sm->sanitizeString($password));
		$email=$sm->sanitizeString($email);
		
		if (!$this->checkUserExists($username)) {
			$adduser=$sm->query("INSERT INTO Sys_users (username, password, email) VALUES ('$username', '$password', '$email')");
			$_SESSION['nouser']=0; // First bug fix x);
			if ($sm->error()!='')
				return _('Error inserting user in dabase: ').$sm->error();
			else 
				return 0;
		}
		else {
			return 2;//_('User allready exists');
		}
		
	}
		
	function countUsers () {
		global $sm;
		$resultado=$sm->query('SELECT COUNT(userid) FROM Sys_users');
		$Res=$sm->fetch();
		return $Res['COUNT(userid)'];
	}
}

?>